<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

$container_id =$_GET['container'];
$page_xml = $_GET['page_xml'];
$existing_xml = $_GET['existing_xml'];

/** this is to render properties */
$component_type=$_GET['component'];
$component_id=$_GET['id'];

if(!isset($semf_root) )
{
	$semf_root = '../';
	$not_starter = 0;
	// Define that we are returning XML content & not to cache:
	header('Content-Type: text/xml');
	header('Cache-control: no-cache');
}

/** undo or redo before opening existing if needed */
include("undo_redo.php");
include_once("util/dirutils.php");
include_once("util/properties.php");
include_once("util/backup.php");

//first check whether it is a existing component..
if($existing_xml != "")
{
	$page_doc =  new DOMDocument();
	$page_doc->load($page_xml);
	$page_doc->validate();
	
	$comp_doc = new DOMDocument();
	$comp_doc->load($existing_xml);
	$comp_doc->validate();
	
	$comp_root = $comp_doc->documentElement;
	$comp_root_type = $comp_root->getAttribute("type");
	
	if($comp_root_type == "Page") //right now it is prohobitted..
	{
		//just replace the page_xml with existing_xml..
		copy($existing_xml, $page_xml);
		$container_id = "";
	}
	else
	{
		if(!isset($container_id) || $container_id==NULL || 
								$container_id == 'null') //this come from js  
		{
			$container = $page_doc->documentElement;
			//$component_name = 'Container'; // this is the root container..
		}
		else
		{
			$container = $page_doc->getElementById($container_id);
			//$component_name = $container->tagName;
		}
		$id = rand() + 3000;
		copy_components($container, $comp_root, $id, $page_doc, $semf_root);
		savePageXML($page_doc, $page_xml);
	}
}

function id_generator($doc)
{
	$ele = NULL;
	$id = NULL;
	do
	{
		$id = rand();
		$ele = $doc->getElementById($id);
	}
	while($ele != NULL);
	return $ele;
}

echo "<message>";

$script_text = "";
$copy_text = "";
// hope to be filled inside "open_existing_component.php"
echo "<set id=\"designerView\">";
include("open_existing_component.php");
echo "</set>";

echo $copy_text; //hope no garbages will return.

echo "<option id=\"propertiesView\">";
if( isset($component_type) )
{
	include("render_properties.php");
}
echo "</option>";

echo "<option id=\"onPageRenderFinish\">";
include("open_existing_info.php");
echo "</option>";

if(!isset($_GET["undo_redo"]) || $_GET["undo_redo"] == "")
{
	if(!isset($_GET["existing_xml"]) || $_GET["existing_xml"] == "")
	{
		if($not_starter == 0) //only at the startup..
		{
			include('load_module_data.php');
		}
	}
}


echo "<script>";
echo $script_text;
echo "</script>";

echo "</message>";

function copy_components($container, $component, $id, $page_doc, $semf_root)
{
	//get properties..
	$properties = load_properties($component);
	$component_type = $component->getAttribute("type");
	
	$new_comp = $page_doc->createElement("component");
	$new_comp->setAttribute("type", $component_type);
	while($page_doc->getElementById($component_type.$id) != NULL)
	{
		$id ++;
	}
	$new_comp->setAttribute("id", $component_type.$id);
	$container->appendChild($new_comp);
	
	include_once("./components/${component_type}/component.php");
	$class_name = ${component_type}."Component";
	$obj = new ${class_name}();
	
	$properties = $obj-> adjust_properties_with_container($container, $properties);
	save_properties($new_comp, $properties, $page_doc);
	
	$new_containers = $obj->build_containers($container, $properties);
	render_containers($new_comp, $new_containers, $page_doc);
	
	$new_container_ids = array_keys($new_containers);
	
	$new_container_index = 0;
	//copy old container components to new one.
	foreach ( $component->childNodes as $child)
	{
		if( $child-> tagName != 'container')
		{
			continue;
		}
		// so the $child is a container..
		$child_component = $child->firstChild;
		if($child_component != NULL)
		{
			$container_id = $new_container_ids[$new_container_index];
			
			$new_container = $page_doc->getElementById($container_id);
			$id = copy_components($new_container, $child_component, ++$id, $page_doc, $semf_root);
		}
		$new_container_index ++;
	}
	return $id;
}

function render_containers($node, $containers, $doc)
{
	$key_node = NULL;
	$container_keys = array_keys($containers);
	$container_attributes = NULL;
	$value = NULL;
	foreach($container_keys as $key)
	{
		$key_node = $doc->createElement('container');
		$key_node-> setAttribute('id', $key);
		
		$container_attributes = $containers[$key];
		$attri_keys = array_keys($container_attributes);
		foreach ($attri_keys as $attri )
		{
			$value = $container_attributes[$attri];
			$key_node-> setAttribute($attri, $value );
		}
		$node->appendChild($key_node);
	}
}